﻿/* EntitiesToDTOs. Copyright (c) 2011. Fabian Fernandez.
 * http://entitiestodtos.codeplex.com
 * Licensed by Common Development and Distribution License (CDDL).
 * http://entitiestodtos.codeplex.com/license
 * Fabian Fernandez. 
 * http://www.linkedin.com/in/fabianfernandezb/en
 * */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using EntitiesToDTOs.Properties;
using System.IO;

namespace EntitiesToDTOs.Helpers
{
    /// <summary>
    /// Helps in the tasks relative to the Assembly.
    /// </summary>
    internal class AssemblyHelper
    {
        /// <summary>
        /// Assembly that contains the code that is currently executing.
        /// </summary>
        public static Assembly ExecutingAssembly
        {
            get
            {
                if (_executingAssembly == null)
                {
                    _executingAssembly = Assembly.GetExecutingAssembly();
                }

                return _executingAssembly;
            }
        }
        private static Assembly _executingAssembly = null;

        /// <summary>
        /// AssemblyName of the Assembly that contains the code that is currently executing.
        /// </summary>
        public static AssemblyName ExecutingAssemblyName
        {
            get
            {
                if (_executingAssemblyName == null)
                {
                    _executingAssemblyName = AssemblyHelper.ExecutingAssembly.GetName();
                }

                return _executingAssemblyName;
            }
        }
        private static AssemblyName _executingAssemblyName = null;

        /// <summary>
        /// Executing Assembly Version in the form of "Major.Minor{.BetaSuffix}".
        /// </summary>
        public static string Version
        {
            get
            {
                if (_version == null)
                {
                    _version = (AssemblyHelper.ExecutingAssemblyName.Version.Major.ToString() 
                        + Resources.Dot 
                        + AssemblyHelper.ExecutingAssemblyName.Version.Minor.ToString()
                        + AssemblyHelper.VersionBeta);
                }

                return _version;
            }
        }
        private static string _version = null;

        /// <summary>
        /// Gets version information of executing assembly.
        /// </summary>
        public static string VersionInfo
        {
            get
            {
                if (_versionInfo == null)
                {
                    var versionInfo = (Attribute.GetCustomAttribute(AssemblyHelper.ExecutingAssembly,
                        typeof(AssemblyInformationalVersionAttribute)) as AssemblyInformationalVersionAttribute);

                    if (versionInfo == null)
                    {
                        // Attribute is not present
                        _versionInfo = string.Empty;
                    }
                    else
                    {
                        _versionInfo = versionInfo.InformationalVersion;
                    }
                }

                return _versionInfo;
            }
        }
        private static string _versionInfo = null;

        /// <summary>
        /// Version Beta info (can be empty).
        /// </summary>
        public static string VersionBeta
        {
            get
            {
                if (_versionBeta == null)
                {
                    if (string.IsNullOrWhiteSpace(AssemblyHelper.VersionInfo) == true)
                    {
                        // No version info
                        _versionBeta = string.Empty;
                    }
                    else
                    {
                        // Split values of version info
                        string[] versionInfoSplitted = AssemblyHelper.VersionInfo
                            .Split(new string[] { Resources.VersionInfoSplitter }, StringSplitOptions.RemoveEmptyEntries);

                        // Search beta suffix info
                        foreach (string info in versionInfoSplitted)
                        {
                            if (info.StartsWith(Resources.VersionInfoBetaSuffix) == true)
                            {
                                // Beta info can be empty
                                _versionBeta = info.Split(new string[] { Resources.VersionInfoBetaSuffix }, 
                                    StringSplitOptions.None)[1];

                                break;
                            }
                        }
                    }
                }

                return _versionBeta;
            }
        }
        private static string _versionBeta = null;

        /// <summary>
        /// Release ID.
        /// </summary>
        public static int ReleaseID
        {
            get
            {
                if (_releaseID.HasValue == false)
                {
                    if (string.IsNullOrWhiteSpace(AssemblyHelper.VersionInfo) == true)
                    {
                        // No version info
                        _releaseID = 0;
                    }
                    else
                    {
                        // Split values of version info
                        string[] versionInfoSplitted = AssemblyHelper.VersionInfo
                            .Split(new string[] { Resources.VersionInfoSplitter }, StringSplitOptions.RemoveEmptyEntries);

                        // Search release id info
                        foreach (string info in versionInfoSplitted)
                        {
                            if (info.StartsWith(Resources.VersionInfoReleaseID) == true)
                            {
                                _releaseID = Convert.ToInt32(info.Split(new string[] { Resources.VersionInfoReleaseID },
                                    StringSplitOptions.None)[1]);

                                break;
                            }
                        }
                    }
                }

                return _releaseID.Value;
            }
        }
        private static int? _releaseID = null;

        /// <summary>
        /// Executing assembly location.
        /// </summary>
        public static string AssemblyLocation
        {
            get
            {
                return AssemblyHelper.ExecutingAssembly.Location;
            }
        }

        /// <summary>
        /// Executing assembly location.
        /// </summary>
        public static string AssemblyLocationPath
        {
            get
            {
                if (_assemblyLocationPath == null)
                {
                    _assemblyLocationPath = Path.GetDirectoryName(AssemblyHelper.ExecutingAssembly.Location) + "\\";
                }

                return _assemblyLocationPath;
            }
        }
        private static string _assemblyLocationPath = null;

    }
}